package com.frxuexi.sign.view

import android.text.TextUtils
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.TimeUtils
import com.frxuexi.lib_base.IUiState
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.lib_base.RouterPath
import com.frxuexi.sign.R
import com.frxuexi.sign.SignEntity
import com.frxuexi.sign.SignIntent
import com.frxuexi.sign.SignViewModel
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

@Composable
fun SignPage(navHostController: NavHostController,vm:SignViewModel= hiltViewModel()) {
    var list = remember {
        SnapshotStateList<SignEntity>()
    }
    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect{
                when(it){
                    is IUiState.onSuccess<*> ->{
                        Log.i("===",it.data.toString())
                        list.addAll(it.data as Collection<SignEntity>)
                    }
                    is IUiState.onError->{
                        Log.i("===",it.msg)
                    }
                    else->{}
                }
            }
        }

        vm.sendIntent(SignIntent.getSigns(mapOf("userId" to 1,"page" to 1)))
    })

    var signState by remember {
        mutableStateOf("签到")
    }
    var isEnable by remember {
        mutableStateOf(true)
    }
    val kv = MMKV.defaultMMKV()
    Scaffold(
        bottomBar = {
            Button(onClick = {
                val currentTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())
                if(signState=="签到"){
                    kv.encode("sign","sign,$currentTime")
                    signState="签退"
                }else{
                    kv.encode("sign","sout,$currentTime")
                    isEnable=false
                }
                navHostController.navigate(RouterPath.SIGNCOMMIT_PAGE)
            },enabled=isEnable,
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(10.dp, 5.dp)) {
                Text(text = signState)
            }
        }
    ) {
        LaunchedEffect(key1 = "", block = {
            //sign,2024-09-26 10:29:30
            //sout,2024-09-26 10:29:30
            val sign = kv.decodeString("sign", "").toString()
            if(TextUtils.isEmpty(sign)){
                signState="签到"
            }
            else{
                val split = sign.split(",")
                val state = split[0]//打卡的状态
                val time = split[1].toString().substring(0, 10)//打卡的日期
                val currentTime = SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis())//获取打开签到页面的日期
                if(time==currentTime){
                    if(state=="sign"){//说明今天打过签到
                        signState="签退"
                    }else{
                        isEnable=false
                    }
                }else{//说明今天没有打过卡
                    signState="签到"
                }
            }
        })


        Column (Modifier.padding(it)){
            MyTitleBar(navHostController = navHostController, title = "考勤")
            Row(horizontalArrangement = Arrangement.SpaceBetween, modifier = Modifier
                .fillMaxWidth()
                .padding(20.dp, 10.dp)) {
                val week = TimeUtils.getChineseWeek(System.currentTimeMillis())
                Text(text = "时间$week")
                Text(text = "考勤规则")
            }
            LazyColumn(modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 20.dp),verticalArrangement = Arrangement.spacedBy(10.dp), content = {
                items(list){
                    val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(it.signTime)
                    Text(text = time,
                        Modifier
                            .fillMaxWidth()
                            .background(colorResource(id = R.color.lightblue))
                            .padding(20.dp))
                }
            })

        }
    }

}